/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class COMMUNICATION_CHANNEL implements Serializable,Cloneable{
    private String source_number;
    private PARTNER partner = new PARTNER();
    private String type;
    private String id;

    public COMMUNICATION_CHANNEL() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSource_number() {
        return source_number;
    }

    public void setSource_number(String source_number) {
        this.source_number = source_number;
    }

    public PARTNER getPartner() {
        return partner;
    }

    public void setPartner(PARTNER partner) {
        this.partner = partner;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    
    public static ArrayList<COMMUNICATION_CHANNEL> getAll(String condition) throws Exception{
        ArrayList<COMMUNICATION_CHANNEL> list = new ArrayList<COMMUNICATION_CHANNEL>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "select * from communication_channels"
                + " "+condition
                + "  order by source_number";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                COMMUNICATION_CHANNEL c = new COMMUNICATION_CHANNEL();
                PARTNER p = new PARTNER();
                c.setSource_number(rs.getString(1));              
                p.setPartner_id(Integer.parseInt(rs.getString(2)));
                c.setPartner(p);
                c.setType(rs.getString(3));
                c.setId(rs.getString(4));
                list.add(c);
            }
            return list;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }
        
    public boolean remove() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        String strSQL = "DELETE FROM communication_channels WHERE ID = ?";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            
            pst.setInt(1, Integer.parseInt(getId()));
            
            return pst.executeUpdate() > 0;            
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    public boolean insert() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;        
        try {
            cn = AppServer.getConnection();            
            setId(Database.getSequenceValue(cn, "seq_channel"));
            String strSQL = "insert into communication_channels(source_number,partner_id,type,id) values(?,?,?,?)";
            pst = cn.prepareStatement(strSQL);
            pst.setString(1, getSource_number());
            pst.setInt(2, getPartner().getPartner_id());
            pst.setString(3, getType());
            pst.setInt(4, Integer.parseInt(getId()));
            return pst.executeUpdate() > 0;            
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    public boolean update() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        String strSQL = "update communication_channels"
                + " set source_number = ?,partner_id = ?,type = ?"
                + " where id = ?";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            pst.setString(1, getSource_number());
            pst.setInt(2, getPartner().getPartner_id());
            pst.setString(3, getType());
            pst.setInt(4, Integer.parseInt(getId()));
            return pst.executeUpdate() > 0;            
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}
